package com.lynn.mmcc.tools;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * 所有的业务日志都从这里面输出
 *    
 * 项目名称：VRSUDP   
 * 类名称：TrackLogger   
 * 类描述：   
 * 创建人：alex   
 * 创建时间：2012-3-22 下午07:50:28   
 * 修改人：alex   
 * 修改时间：2012-3-22 下午07:50:28   
 * 修改备注：   
 * @version    
 *
 */
public class BusinessLogger {
    
    private static Logger logger = Logger.getLogger ( BusinessLogger.class ) ; 
    
//    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd:HHmmss");
    private static SimpleDateFormat sdfShort = new SimpleDateFormat("yyyyMMdd");
    
    private static final Object buildLock = new Object();
    
    private static long count = 0;
    
    private static Date origin = new Date();
    
    /**
     * 获得操作序列号  每天开始从头开始
     * @return
     */
    public static String bulidOpSerial()
    {
	synchronized (buildLock) {
	    Date now = new Date();
	    if(!sdfShort.format(now).equals(sdfShort.format(origin)))
	    {
		count = 0;
		origin = now;
	    }
	    return "" + ++count;
	}
    }
    
    /**
     * 此方法用来格式化业务信息的
     * @param requestSerial 请求序列号
     * @param usrid   用户ID
     * @param interfaceName  接口名称
     * @param startFlag  开始结束标记   true 表开始  false表结束
     * @param opResult   操作结果   只有结束的时候才填这个值
     * @param opSerial   操作序列号
     * @return
     */
    public static void LoggerInfo(String requestSerial,String usrid,String component,String interfaceName,boolean startFlag,String opResult,int index,String opSerial)
    {
	StringBuilder info = new StringBuilder();
	info.append(opSerial==null?"":opSerial);
	info.append("|");
	info.append(usrid==null?"":usrid);
	info.append("|");
	info.append(component==null?"":component);
	info.append("|");
	info.append(interfaceName==null?"":interfaceName);
	info.append("|");
	if(startFlag)
	{
	    info.append("S|");
	    //info.append(sdf.format(new Date()));
	    info.append(new Date().getTime());
	    info.append("|");
	    info.append(opResult==null?"":opResult);
	    info.append("|");
	}else{
	    info.append("E|");
	    //info.append(sdf.format(new Date()));
	    info.append(new Date().getTime());
	    info.append("|");
	    info.append(opResult==null?"":opResult);
	    info.append("|");
	}
	info.append(index);
	logger.info(info.toString());
    }    
    
    public static void info(Object message){
	logger.info(message);
    }
    
    public static void info(Object message,Throwable t){
	logger.info(message,t);
    }
    
    public static void debug(Object message){
	logger.debug(message);
    }
    
    public static void debug(Object message,Throwable t){
	logger.debug(message,t);
    }
    
    public static void warn(Object message){
	logger.warn(message);
    }
    
    public static void warn(Object message,Throwable t){
	logger.warn(message,t);
    }
    
    public static void error(Object message){
	logger.error(message);
    }
    
    public static void error(Object message,Throwable t){
	logger.error(message,t);
    }
    
    public static void trace(Object message){
	logger.trace(message);
    }
    
    public void trace(Object message,Throwable t){
	logger.trace(message,t);
    }
}
